package com.anjbo.common;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UserAgentUtil {
	private static final String UA_APPLE_KEY = "(iphone)|(mac)|(ios)|(ipad)|(ipod)";
	private static final String UA_ANDROID_KEY = "(android)|(linux)";
	private static final String UA_SYMBIAN_KEY = "(Symbian)|(series)|(nokia)|(java)";
	private static final String UA_J2ME = "(MIDP)";
	private static final String UA_SYMBIAN_S60V3_KEY = "(6120c)|(5320di)|(E73)|(E72)|(E71)|(E70)|(5500)|(3250)|(6710)|(6760s)|(6730C)|(N85)|(N86)|(N81)|(N80)|(N83)|(N82)|(6121c)|(E60)|(E61)|(E62)|(E63)|(E65)|(E66)|(6120ci)|(6650f)|(6720c)|(6650)|(6650t)|(N93i)|(6790surge)|(N92)|(N93)|(N91)|(N95)|(E55)|(6290)|(E51)|(E50)|(6122c)|(E72i)|(6788)|(5500d)|(E75)|(E5)|(5710xm)|(6220c)|(5730XM)|(N78)|(X5-01)|(X5-00)|(6124c)|(6788i)|(5700)|(C5)|(6700S)|(C5-01)|(N96)|(5320xm,5320)|(N75)|(N76)|(N77)|(N71)|(N73)|(N79)|(6210s)|(6210n)|(5630XM)|(E90)|(E5-00)|(E52)|(6790slide)|(E61i)|(6710n)|(6702s)|(5630)|(6110Navigator)";
	private static final String UA_SYMBIAN_S60V5_KEY = "(series60/5)|(5802XM)|(E7-00)|(E77)|(N97mini)|(5900)|(E8)|(5228)|(700)|(701)|(5250)|(E6)|(5530XM)|(N11)|(C7-00)|(T7-00)|(5233)|(5238)|(5230)|(5232)|(5235)|(5236)|(X6)|(X7)|(C5-06)|(C5-03)|(N8)|(N97)|(X6m)|(5800)|(5800XM)|(6110)|(C6-01)|(C6-00)|(600)|(603)|(C5-05)|(N97i)|(5800iXM)|(U1I)|(U5i)|(U1)|(U-1)|(U8i)|(i8910U)|(i8910 HD)";
	private static final String UA_WINDOWS = "Windows NT";
	private static final Logger log = Logger.getLogger(UserAgentUtil.class);
	/**
	 * 判断是否为微信浏览器
	 * @author limh
	 * @date 2015-8-28 下午05:09:06
	 * @param userAgent
	 * @return
	 */
	public static boolean isWeiXin(String userAgent){
		if (StringUtils.isBlank(userAgent)) {
			return false;
		}
		userAgent = userAgent.toLowerCase();
		if (userAgent.indexOf("micromessenger") > 0) {
	        return true;
	    }
		return false;
	}
	/**
	 * 判断是手机平台
	 * 
	 * @param userAgent
	 *            ua
	 * @return
	 */
	public static boolean isMobile(String userAgent) {
		if (StringUtils.isBlank(userAgent)) {
			return false;
		}
		String mobileKey = UA_SYMBIAN_KEY + "|" + UA_APPLE_KEY + "|"
				+ UA_ANDROID_KEY + "|" + UA_J2ME;
		Pattern mobile = Pattern.compile(mobileKey, Pattern.CASE_INSENSITIVE);
		Matcher matcher = mobile.matcher(userAgent);
		boolean find = matcher.find();
		if (find) {
			log.info("匹配到手机平台 关键字= " + matcher.group(0));
		}
		return find;
	}

	public static boolean isWindows(String userAgent) {
		if (StringUtils.isBlank(userAgent)) {
			return false;
		}
		Pattern mobile = Pattern.compile(UA_WINDOWS, Pattern.CASE_INSENSITIVE);
		Matcher matcher = mobile.matcher(userAgent.toLowerCase());
		boolean find = matcher.find();
		if (find) {
			log.info("匹配到windows平台 关键字= " + matcher.group(0));
		}
		return find;
	}

	public static boolean isSymbian(String userAgent) {
		if (StringUtils.isBlank(userAgent)) {
			return false;
		}
		Pattern symbian = Pattern.compile(UA_SYMBIAN_KEY,
				Pattern.CASE_INSENSITIVE);
		Matcher matcher = symbian.matcher(userAgent);
		boolean mobile = matcher.find();
		if (mobile) {
			log.info("匹配到symbian 关键字= " + matcher.group(0) + " ua= "
					+ userAgent);
		}
		return mobile;
	}

	public static boolean isj2me(String userAgent) {
		if (StringUtils.isBlank(userAgent)) {
			return false;
		}
		Pattern symbian = Pattern.compile(UA_J2ME, Pattern.CASE_INSENSITIVE);
		Matcher matcher = symbian.matcher(userAgent);
		boolean mobile = matcher.find();
		if (mobile) {
			log.info("匹配到j2me 关键字= " + matcher.group(0) + " ua= " + userAgent);
		}
		return mobile;
	}

	public static boolean isS60v3(String userAgent) {
		if (StringUtils.isBlank(userAgent)) {
			return false;
		}
		Pattern symbianS60V3 = Pattern.compile(UA_SYMBIAN_S60V3_KEY,
				Pattern.CASE_INSENSITIVE);
		Matcher matcher = symbianS60V3.matcher(userAgent);
		boolean mobile = matcher.find();
		if (mobile) {
			log.info("匹配到SymbianS60v3 关键字= " + matcher.group(0) + " ua= "
					+ userAgent);
		}
		return mobile;
	}

	public static boolean isS60v5(String userAgent) {
		if (StringUtils.isBlank(userAgent)) {
			return false;
		}
		final Pattern symbianS60V5 = Pattern.compile(UA_SYMBIAN_S60V5_KEY,
				Pattern.CASE_INSENSITIVE);
		Matcher matcher = symbianS60V5.matcher(userAgent);
		boolean mobile = matcher.find();
		if (mobile) {
			log.info("匹配到SymbianS60v5 关键字= " + matcher.group(0) + " ua= "
					+ userAgent);
		}
		return mobile;
	}

	/**
	 * 适配iphone手机终端
	 * 
	 * @author Jerry
	 * @version v1.0 2013-10-25 下午03:09:56
	 * @param userAgent
	 * @return
	 */
	public static boolean isIphone(String userAgent) {
		if (StringUtils.isBlank(userAgent)) {
			return false;
		}
		Pattern wapIphone = Pattern.compile(UA_APPLE_KEY,
				Pattern.CASE_INSENSITIVE);
		Matcher matcher = wapIphone.matcher(userAgent);
		boolean mobile = matcher.find();
		if (mobile) {
			log.info("匹配到Iphone 关键字= " + matcher.group(0) + " ua= " + userAgent);
		}
		return mobile;
	}

	public static boolean isAndroid(String userAgent) {
		if (StringUtils.isBlank(userAgent)) {
			return false;
		}
		Pattern wapAndriod = Pattern.compile(UA_ANDROID_KEY,
				Pattern.CASE_INSENSITIVE);
		Matcher matcher = wapAndriod.matcher(userAgent);
		boolean mobile = matcher.find();
		if (mobile) {
			log.info("匹配到Andriod 关键字= " + matcher.group(0) + " ua= "
					+ userAgent);
		}
		return mobile;
	}

	public static boolean isWapWml(String userAgent) {
		if (StringUtils.isBlank(userAgent)) {
			return false;
		}
		String wmlKey = UA_J2ME + "|" + UA_SYMBIAN_KEY + "|"
				+ UA_SYMBIAN_S60V3_KEY;
		Pattern wml = Pattern.compile(wmlKey, Pattern.CASE_INSENSITIVE);
		Matcher matcher = wml.matcher(userAgent);
		boolean mobile = matcher.find();
		if (mobile) {
			log.debug("匹配到手机wml 关键字= " + matcher.group(0) + " ua= " + userAgent);
		}
		return mobile;
	}

	public static boolean isWapHtml(String userAgent) {
		if (StringUtils.isBlank(userAgent)) {
			return false;
		}
		String wapHtmlKey = UA_ANDROID_KEY + "|" + UA_APPLE_KEY + "|"
				+ UA_SYMBIAN_S60V5_KEY + "|" + UA_WINDOWS;
		Pattern wapHtml = Pattern.compile(wapHtmlKey, Pattern.CASE_INSENSITIVE);
		Matcher matcher = wapHtml.matcher(userAgent.toLowerCase());
		boolean mobile = matcher.find();
		if (mobile) {
			log.info("匹配到手机html 关键字= " + matcher.group(0) + " ua= " + userAgent);
		}
		return mobile;
	}



	/**
	 * 获取客户端浏览器信息
	 *
	 * @return string
	 */
	public static String getBrowserInfo(String userAgent) {

		String browserInfo = "other";
		String ua = userAgent.toLowerCase();
		String s;
		String version;
		String msieP = "msie ([\\d.]+)";
		String ieheighP = "rv:([\\d.]+)";
		String firefoxP = "firefox\\/([\\d.]+)";
		String chromeP = "chrome\\/([\\d.]+)";
		String operaP = "opr.([\\d.]+)";
		String safariP = "version\\/([\\d.]+).*safari";

		Pattern pattern = Pattern.compile(msieP);
		Matcher mat = pattern.matcher(ua);
		if (mat.find()) {

			s = mat.group();
			version = s.split(" ")[1];
			browserInfo = "ie " + version.substring(0, version.indexOf("."));
			return browserInfo;

		}

		pattern = Pattern.compile(firefoxP);
		mat = pattern.matcher(ua);
		if (mat.find()) {

			s = mat.group();
			version = s.split("/")[1];
			browserInfo = "firefox "
					+ version.substring(0, version.indexOf("."));
			return browserInfo;

		}

		pattern = Pattern.compile(ieheighP);
		mat = pattern.matcher(ua);
		if (mat.find()) {

			s = mat.group();
			version = s.split(":")[1];
			browserInfo = "ie " + version.substring(0, version.indexOf("."));
			return browserInfo;

		}

		pattern = Pattern.compile(operaP);
		mat = pattern.matcher(ua);
		if (mat.find()) {

			s = mat.group();
			version = s.split("/")[1];
			browserInfo = "opera " + version.substring(0, version.indexOf("."));
			return browserInfo;

		}

		pattern = Pattern.compile(chromeP);
		mat = pattern.matcher(ua);
		if (mat.find()) {

			s = mat.group();
			version = s.split("/")[1];
			browserInfo = "chrome "
					+ version.substring(0, version.indexOf("."));
			return browserInfo;

		}

		pattern = Pattern.compile(safariP);
		mat = pattern.matcher(ua);
		if (mat.find()) {

			s = mat.group();
			version = s.split("/")[1].split(" ")[0];
			browserInfo = "safari "
					+ version.substring(0, version.indexOf("."));
			return browserInfo;

		}
		return browserInfo;

	}

	/**
	 * 获取客户端操作系统信息
	 * 
	 * @param request
	 * @return string
	 */
	public static String getClientOS(String userAgent) {
		try {
			String cos = "unknow os";
			Pattern p = Pattern.compile(".*(Windows NT 6\\.2).*");
			Matcher m = p.matcher(userAgent);
			if (m.find()) {

				cos = "Win 8";
				return cos;

			}

			p = Pattern.compile(".*(Windows NT 6\\.1).*");
			m = p.matcher(userAgent);
			if (m.find()) {

				cos = "Win 7";
				return cos;

			}

			p = Pattern.compile(".*(Windows NT 5\\.1|Windows XP).*");
			m = p.matcher(userAgent);
			if (m.find()) {

				cos = "WinXP";
				return cos;

			}

			p = Pattern.compile(".*(Windows NT 5\\.2).*");
			m = p.matcher(userAgent);
			if (m.find()) {

				cos = "Win2003";
				return cos;

			}

			p = Pattern.compile(".*(Win2000|Windows 2000|Windows NT 5\\.0).*");
			m = p.matcher(userAgent);
			if (m.find()) {

				cos = "Win2000";
				return cos;

			}

			p = Pattern.compile(".*(Mac|apple|MacOS8).*");
			m = p.matcher(userAgent);
			if (m.find()) {

				cos = "MAC";
				return cos;

			}

			p = Pattern.compile(".*(WinNT|Windows NT).*");
			m = p.matcher(userAgent);
			if (m.find()) {

				cos = "WinNT";
				return cos;

			}

			p = Pattern.compile(".*Linux.*");
			m = p.matcher(userAgent);
			if (m.find()) {

				cos = "Linux";
				return cos;

			}
			p = Pattern.compile(".*(68k|68000).*");
			m = p.matcher(userAgent);
			if (m.find()) {

				cos = "Mac68k";
				return cos;

			}
			p = Pattern
					.compile(".*(9x 4.90|Win9(5|8)|Windows 9(5|8)|95/NT|Win32|32bit).*");
			m = p.matcher(userAgent);
			if (m.find()) {

				cos = "Win9x";
				return cos;

			}
			return cos;
		} catch (Exception e) {
			e.printStackTrace();
		}
return null;
	}
}
